﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace PIM_ATE.Mods
{
    class ClsLocalRecord
    {
        public static string sCopyPath = "D:\\PIM-ATE-Report";
        public static string sCopyPathSecondTest = "D:\\交调复测数据";
        string separator = ",";
        string LF = "\r\n";
        string separator3 = "|";
        string sShiftTime;
        string sTestStep;
        string sTestStatue;
        string sLocalStep;
        string sProcode;
        string sProductSN;

        Dictionary<int, JcPIMRecordResult> dicResult;
        ClsDataUser user;
        List<ClsDataConfig> config;
        DateTime dt;

        public ClsLocalRecord(ClsDataProduct product, DateTime dt,
                              Dictionary<int, JcPIMRecordResult> result,
                              ClsDataUser m_userObj, List<ClsDataConfig> config)
        {
            this.sTestStep = product.sPIMTestStep;
            this.sProductSN = product.sProductSN;
            this.sProcode = product.sProcode;
            this.sLocalStep = product.sLocalStep;
            this.sTestStatue = "";
            this.sShiftTime = PIMFunction.GetPIMShiftTime(dt);
            this.user = m_userObj;
            this.dicResult = result;
            this.config = config;
            this.dt = dt;
        }

        public bool SaveLocalFile(int mode, ModTestMode TestMode = ModTestMode.First)
        {
            string fileName = sProcode + "-" + sProductSN + dt.ToString("-yyyyMMddHHmm");
            string filePath = Application.StartupPath + "\\local\\";
            string copypath = sCopyPath;
            if (TestMode == ModTestMode.Second)
            {
                filePath = Application.StartupPath + "\\local-SecondTest\\";
                copypath = sCopyPathSecondTest;
            }          
            if (mode == 1)
                filePath += "eng";              
            else
                filePath += "pim\\";
            if (!Directory.Exists(filePath)) Directory.CreateDirectory(filePath);
            if (!Directory.Exists(copypath)) Directory.CreateDirectory(copypath);
            if (mode == 1) filePath += "\\eng-";
            else filePath += "\\";
            try
            {
                string sResultFileName = config[0].IsSweepTest ? "(扫频)产品测试记录.csv" : "(点频)产品测试记录.csv";
                SaveProductResultRec(filePath + sProcode + sResultFileName);

                SaveProductTestInfo(filePath + fileName + "-Info.csv");

                SaveRawData(filePath + fileName + "-Data.txt");

                //if (!GDefine.IS_COMPAT_DEBUG_TEST)
                //{
                    File.Copy(filePath + sProcode + sResultFileName,
                            copypath + "\\" + sProcode + sResultFileName, true);
                        File.Copy(filePath + fileName + "-Info.csv",
                           copypath + "\\" + fileName + "-Info.csv", true);
                //}                  
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("SaveLocalFile失败: " + ex.Message);
                return false;
            }
        }

        public void SaveProductTestInfo(string fileName)
        {
            StreamWriter sw = new StreamWriter(fileName, false, Encoding.Default);
            string title = "序列号,测试员,测试时间,班次,测试流程,结果判定,通道";
            sw.WriteLine(title);
            string valuePath = "";
            for (int i = 0; i < config.Count; i++)
            {
                if (!config[i].Enable) continue;

                valuePath += sProductSN + separator +
                        user.sUserID + separator +
                        dt.ToString("yyyy/MM/dd HH:mm") + separator +
                        sShiftTime + separator +
                        sTestStep + separator +
                        dicResult[i].sPass + separator +
                        "Path" + (i + 1) + LF;
            }
            sw.WriteLine(valuePath);

            string valueData = "";
            for (int i = 0; i < config.Count; i++)
            {
                if (!config[i].Enable) continue;

                int n = dicResult[i].index;
                valueData += "Path" + (i + 1).ToString() + ",,,校正前的数据,,,,,,校正后的数据,check值" + LF;
                JcPIMDataResults[] aResult = dicResult[i].valus_knock == null ? dicResult[i].valus : dicResult[i].valus_knock;
                valueData += aResult[n].F1.ToString() + separator +
                         aResult[n].F2.ToString() + separator +
                         aResult[n].Im_F.ToString() + separator +
                         aResult[n].Im_V_NotFix.ToString() + separator +
                         config[i].PIMOrder.ToString() + separator +
                         aResult[n].P1.ToString() + separator +
                         aResult[n].P2.ToString() + separator +
                         "\"" + config[i].SpecReg + "\"" + separator +
                         dicResult[i].sPass + separator +
                         aResult[n].Im_V + separator +
                         config[i].Check + separator + LF;

                if (config[i].IsSweepTest)
                {
                    valueData += "F1(MHz),F2(MHz),PIM(MHz),Result,Order,Pwr1,Pwr2,Ref,AddValue,Rnd[]" + LF;
                    for (int j = 0; j < dicResult[i].valus.Length; j++)
                    {
                        valueData += dicResult[i].valus[j].F1 + separator +
                                 dicResult[i].valus[j].F2 + separator +
                                 dicResult[i].valus[j].Im_F + separator +
                                 dicResult[i].valus[j].Im_V_NotFix + separator +
                            //dicResult[i].valus[j].Im_V + separator +
                                 config[i].PIMOrder + separator +
                                 dicResult[i].valus[j].P1 + separator +
                                 dicResult[i].valus[j].P2 + separator +
                                  "\"" + config[i].SpecReg + "\"" + separator +
                                  dicResult[i].valus[j].Im_V + separator +
                            //dicResult[i].valus[j].Im_V_NotFix + separator +
                                 config[i].Random + LF;
                    }
                }
            }
            sw.WriteLine(valueData);
            sw.Close();
        }

        public void SaveProductResultRec(string fileName)
        {

            string sAllPass = GDefine.ContentPass2;
            string sTitle = "";
            string sRow = "";

            sTitle = "序列号,测试员,测试时间,班次,测试流程,";
            for (int i = 0; i < config.Count; i++)
            {
                sTitle += config[i].ProCH + "测试值" + separator +
                         config[i].ProCH + "判定结果" + separator;
                if (config[i].IsSweepTest)
                    sTitle += config[i].ProCH + "测试过程中每个频点结果" + separator;
            }
            sTitle += "产品判定结果" + separator;
            sTitle += "备注";

            sRow = sProductSN + separator + user.sUserID + separator +
                   dt.ToString("yyyy/MM/dd HH:mm:ss") + separator +
                   sShiftTime + separator + sTestStep + separator;
            for (int i = 0; i < config.Count; i++)
            {
                if (config[i].Enable)
                {
                    sRow += dicResult[i].fValue + separator +
                             dicResult[i].sPass + separator;

                    if (dicResult[i].sPass == GDefine.ContentFail2)
                        sAllPass = dicResult[i].sPass;
                }
                else
                {
                    sRow += "" + separator + "" + separator;
                }

                if (config[i].IsSweepTest)
                {
                    if (config[i].Enable)
                    {
                        sRow += "\"(";
                        for (int j = 0; j < dicResult[i].valus.Length; j++)
                        {
                            sRow += dicResult[i].valus[j].Im_V.ToString();
                            if (j != dicResult[i].valus.Length - 1)
                            {
                                sRow += ",";
                            }
                        }
                        sRow += ")\"" + separator;
                    }
                    else
                    {
                        sRow += "" + separator;
                    }
                }
            }
            sRow += sAllPass + separator;
            sRow += sLocalStep;

            bool isExsit = File.Exists(fileName);
            FileStream fsFreq = new FileStream(fileName, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
            StreamWriter swFreq = new StreamWriter(fsFreq, Encoding.Default);
            if (!isExsit)
            {//输出表头
                swFreq.WriteLine(sTitle);
            }
            swFreq.WriteLine(sRow);
            swFreq.Close();
        }

        public void SaveRawData(string fileName)
        {
            double dMaxVaule = double.MinValue;
            int index = 0;
            for (int i = 0; i < config.Count; i++)
            {
                if (!config[i].Enable) continue;
                if (dMaxVaule < dicResult[i].fValue)
                {
                    dMaxVaule = dicResult[i].fValue;
                    index = i;
                }
            }

            string sResult = "";
            if (PIMFunction.compareSpec(dMaxVaule, config[index].SpecReg))
                sResult = GDefine.ContentPass2;
            else
                sResult = GDefine.ContentFail2;
            string sSpec = config[index].SpecReg;

            string mh = "\"";
            StreamWriter sw = new StreamWriter(fileName, false, Encoding.Default);
            string sTitle = "";
            sTitle += "\"S代码\" \"腔体序列号\" \"测试员工号\" \"测试日期\" \"班次\" \"状态标识\" \"过程标识\" \"测试要求值\" \"测试值\" \"测试结果\"\r\n";
            sTitle += mh + sProcode + mh +
                     "  " +
                     mh + sProductSN + mh +
                     "  " +
                     mh + user.sUserID + mh +
                     "  " +
                     mh + dt.ToString("yyyy/MM/dd HH:mm:ss") + mh +
                     "  " +
                     mh + sShiftTime + mh +
                     "  " +
                     mh + sTestStatue + mh +
                     "  " +
                     mh + sTestStep + mh +
                     "  " +
                     mh + sSpec + mh +
                     "  " +
                     mh + dMaxVaule + mh +
                     "  " +
                     mh + sResult + mh +
                     "  ";

            sw.WriteLine(sTitle);


            for (int i = 0; i < config.Count; i++)
            {
                if (!config[i].Enable) continue;

                string value = "";
                for (int j = 0; j < dicResult[i].valus.Length; j++)
                {
                    value += config[i].ProCH + separator3 +
                             "f1=" + dicResult[i].valus[j].F1.ToString() + " " +
                             "f2=" + dicResult[i].valus[j].F2.ToString() + " " +
                             "PIM=" + dicResult[i].valus[j].Im_F.ToString() + separator3 +
                             (j + 1).ToString() + separator3 +
                             dicResult[i].valus[j].Im_V.ToString() + LF;

                }

                if (dicResult[i].valus_knock != null)
                {
                    for (int j = 0; j < dicResult[i].valus_knock.Length; j++)
                    {
                        value += config[i].ProCH + separator3 +
                                 "f1=" + dicResult[i].valus_knock[j].F1.ToString() + " " +
                                 "f2=" + dicResult[i].valus_knock[j].F2.ToString() + " " +
                                 "PIM=" + dicResult[i].valus_knock[j].Im_F.ToString() + separator3 +
                                 "0" + separator3 +
                                 dicResult[i].valus_knock[j].Im_V.ToString() + LF;

                    }
                }

                sw.WriteLine(value);
            }

            sw.Close();
        }
    }
}
